package com.xauto.pioneer.appuser.activity.mine.user;

import android.view.View;
import android.widget.EditText;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;

/***
 * 修改昵称
 * @author Lemon Mon 27 Feb 16:02
 */
public class EditNicknameActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    @Bind(R.id.etNickname) EditText etNickname;

    /** 未修改的会员名称*/
    private String memberName;

    @Override
    protected int getContentView() {
        return R.layout.ac_edit_nickname;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, R.string.edit_nickname);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        memberName = getIntent().getStringExtra(Constants.EDIT_TYPE_MEMBER_NAME);
        etNickname.setText(memberName);

        lHandler = new BaseLHttpHandler(mContext, true, true) {

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                prompt("修改成功");
                setResult(Constants.RESULT_EDIT_SUCCESS);
                finish();
            }
        };
    }

    @OnClick(R.id.tvNicknameSubmit)
    public void onClick() {
        if(!StringUtils.isBlank(etNickname.getText().toString())) {
            if(!memberName.equals(etNickname.getText().toString())) {
                loadEditMember(Constants.EDIT_TYPE_MEMBER_NAME, etNickname.getText().toString());
            } else {
                onBackPressed();
            }
        } else {
            prompt("昵称不能为空");
        }
    }

    void loadEditMember(String type, String value) {
        new LHttpLib().editMemberInfo(mContext, memberId, type, value, "", lHandler);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
